package com.zhhtech.zhh.ui.ext

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.fastjson.JSON
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadService
import com.zhhtech.zhh.ui.R
import com.zhhtech.zhh.ui.helper.loadsir.EmptyCallback
import com.zhhtech.zhh.ui.helper.loadsir.ErrorCallback
import com.zhhtech.zhh.ui.helper.loadsir.LoadingCallback
import com.zhhtech.zhh.ui.utils.DEFAULT_PATTERN
import com.zhhtech.zhh.ui.utils.Logger
import java.io.File
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

fun CharSequence?.clipboard(successText: String = "复制成功") {
    if (this == null) return
    try {
        val cm = com.zhhtech.zhh.ui.AppCore.app.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val mClipData = ClipData.newPlainText("Label", this)
        cm.setPrimaryClip(mClipData)
        toastMessageLong(successText)
    } catch (e: Exception) {
        Logger.e(e)
    }
}

fun Context?.goActicity(clazz: Class<*>?, bundle: Bundle? = null, requestCode: Int? = null) {
    if (this == null || clazz == null) return
    val intent = Intent(this, clazz)
    if (bundle != null)
        intent.putExtras(bundle)
    if (requestCode != null && this is Activity) {
        startActivityForResult(intent, requestCode)
    } else {
        startActivity(intent)
    }
}

fun Context?.goActicityFinish(clazz: Class<*>, bundle: Bundle? = null, requestCode: Int? = null) {
    goActicity(clazz, bundle, requestCode)
    if (this is Activity)
        finish()
}

fun View.visible() = apply { visibility = View.VISIBLE }
fun View.gone() = apply { visibility = View.GONE }
fun View.invisible() = apply { visibility = View.INVISIBLE }
fun <T> LoadService<T>.showEmptyCallback() = showCallback(EmptyCallback::class.java)
fun <T> LoadService<T>.showErrorCallback() = showCallback(ErrorCallback::class.java)
fun <T> LoadService<T>.showLoadingCallback() = showCallback(LoadingCallback::class.java)
fun Double.decimalFormat() = DecimalFormat("0.#").format((this))
fun Int.toColor() = ContextCompat.getColor(com.zhhtech.zhh.ui.AppCore.app, this)
val Int?.dp get() = dp2px(this ?: 0)
fun Int.getString() = com.zhhtech.zhh.ui.AppCore.app.getString(this)
fun Int.toastMessageLong() = toastMessageLong(this)
fun String.toastMessageLong() = toastMessageLong(this)
val Any?.log get() = Logger.e(this?.toString())
fun Any.objectClearNull() = JSON.parseObject(JSON.toJSONString(this))
fun Double?.format() = this?.let { DecimalFormat("0.##").format(this) } ?: ""
fun Double?.formatRetain() = this?.let { DecimalFormat("0.00").format(this) } ?: ""

@JvmField
val DEFAULT_DF = SimpleDateFormat(DEFAULT_PATTERN)
fun Date.toYYYYMMDD() = DEFAULT_DF.format(this)
fun Date.format(pattern: String = DEFAULT_PATTERN) =
    if (pattern == DEFAULT_PATTERN)
        toYYYYMMDD()
    else
        SimpleDateFormat(pattern).format(this)

fun <T> LoadService<T>.showLoadingBeforeSuccessCallback() {
    if (currentCallback != SuccessCallback::class.java) showCallback(LoadingCallback::class.java)
}

fun TextView.required() {
    textSpan(text.toString()) {
        spanText = "*"
        addForegroundColorSpan(R.color.alertRed.toColor())
    }
}

fun Context.checkAppInstalled(pkgName: String?): Boolean {
    if (pkgName == null || pkgName.isEmpty()) {
        return false
    }
    var packageInfo: PackageInfo?
    try {
        packageInfo = packageManager.getPackageInfo(pkgName, 0)
    } catch (e: PackageManager.NameNotFoundException) {
        packageInfo = null
        e.printStackTrace()
    }
    return packageInfo != null
}

fun Context.startAPP(appPackageName: String?): Boolean {
    return try {
        val intent = packageManager.getLaunchIntentForPackage(appPackageName!!)
        startActivity(intent)
        true
    } catch (e: Exception) {
        //Toast.makeText(context, "请先安装app", Toast.LENGTH_LONG).show();
        false
    }
}

fun Context.getAppRootPath(): File {
    return if (sdCardIsAvailable()) {
        Environment.getExternalStorageDirectory()
    } else {
        filesDir
    }
}

fun sdCardIsAvailable(): Boolean {
    return if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
        Environment.getExternalStorageDirectory().canWrite()
    } else false
}